#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <unistd.h>

int main(void)
{
    char ch;
    int ret = -1000;
    int recv_len;
    char buf[BUFSIZ];

    struct sockaddr_in server_addr;
    struct sockaddr_in client_addr;
    int client_addr_len = sizeof(struct sockaddr_in);

    int server_sockfd = socket(AF_INET, SOCK_DGRAM, 0);

    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = htons(9000);

    ret = bind(server_sockfd, (struct sockaddr*)&server_addr,sizeof(server_addr));
    if (ret == -1)
    {
        perror("bind");
        exit(1);
    }
    
    while (1)
    {
        printf("server waiting\n");
        recv_len = recvfrom(server_sockfd, buf, sizeof(buf), 0, (struct sockaddr*)&client_addr, &client_addr_len);
        if (recv_len < 0)
        {
            perror("recvfrom");
            exit(-1);
        }
        
        printf("received:%s\n",buf);

        int i=0;
        while(buf[i]) 
        {
            buf[i] = toupper(buf[i]);
            i++;
        }
        printf("change buf is %s\n",buf);

        int send_len = sendto(server_sockfd, buf, strlen(buf)+1, 0, (struct sockaddr*)&client_addr,client_addr_len);
        if (send_len)
        {
            perror("sendto");
            break;
        }  	
    }
    close(server_sockfd);
    return 0;
}


